package com.yeyks.activity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yeyks.activity.dal.dao.ActivityNodeUserRecordMapper;
import com.yeyks.activity.dal.domain.ActivityNodeUserRecord;
import com.yeyks.activity.param.ActivityNodeUserRecordSaveByActivityParam;
import com.yeyks.activity.service.ActivityNodeUserRecordService;
import com.yeyks.common.result.activity.ActivityCardStatResult;
import com.yeyks.common.result.activity.ActivityStatForwardUserResult;
import com.yeyks.common.result.activity.ActivityStatRedResult;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-12-06
 */
@Service
public class ActivityNodeUserRecordServiceImpl extends ServiceImpl<ActivityNodeUserRecordMapper, ActivityNodeUserRecord> implements ActivityNodeUserRecordService {

    //存储节点的参与人
    @Override
    public Boolean saveByActivity(ActivityNodeUserRecordSaveByActivityParam param) {
        try {
            ActivityNodeUserRecord activityNodeUserRecord = BeanUtil.toBean(param, ActivityNodeUserRecord.class);
            return save(activityNodeUserRecord);
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 节点红包领取个数
     *
     * @param activityCode
     * @return
     */
    @Override
    public Integer getRedGetCount(String activityCode) {
        Integer count = baseMapper.getRedGetCount(activityCode);
        return null == count ? 0 : count;
    }

    /**
     * 统计各节点红包的领取个数
     *
     * @param activityCode
     * @return
     */
    @Override
    public List<ActivityStatRedResult> redTotal(String activityCode) {
        return baseMapper.redTotal(activityCode);
    }

    /**
     * 活动转发人数统计
     *
     * @param activityCode
     * @return
     */
    @Override
    public List<ActivityStatForwardUserResult> forwardUserTotal(String activityCode) {
        List<ActivityStatRedResult> activityStatRedResults = baseMapper.redTotal(activityCode);
        List<ActivityStatForwardUserResult> results = Lists.newArrayList();
        for (ActivityStatRedResult activityStatRedResult : activityStatRedResults) {
            ActivityStatForwardUserResult result = new ActivityStatForwardUserResult();
            result.setForwardUserCount(activityStatRedResult.getUserCount());
            result.setPlatformType(activityStatRedResult.getTotal());
            results.add(result);
        }
        return results;
    }

    /**
     * 根据activity_code和node统计
     *
     * @param activityCode
     * @param node
     * @return
     */
    @Override
    public List<ActivityCardStatResult> stat(String activityCode, String node) {
        return baseMapper.stat(activityCode, node);
    }

}
